package LeetCode.TwoPointers;

/**
 * @author : LdLtd
 * @Date : 2023/7/19
 * @Description:11. 盛最多水的容器
 */
public class container_with_most_water {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] ints = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        System.out.println(solution.maxArea1(ints));
    }
    static class Solution {
        /*双循环，超时*/
        public int maxArea1(int[] height) {
            int res=0;
            for (int i = 0; i <height.length; i++) {
                for (int j = i+1; j < height.length; j++) {
                    res=Math.max((j-i)*Math.min(height[i],height[j]),res);
                }
            }
            return res;
        }
        /*双指针
        * 底不变，所以每次移动小的那一个指针*/
        public int maxArea2(int[] height){
            int n = height.length;
            int res=0;
            int l=0,r=n-1;
            while(l<r){
                res=Math.max(res,(r-l)*Math.min(height[l],height[r]));
                if(height[l]>height[r]) --r;
                else ++l;
            }
            return res;
        }


    }
}
